<?php

# PMB Online
# Aplikasi PMB Online untuk Pendaftaran Calon Mahasiswa di PT/PTS Indonesia
# 
# version 1.0
# class app
# file
# created Jan 23, 2013 6:23:08 AM
# 
# (c)2012, arif.laksito@gmail.com

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Apps extends CI_Controller {

    var $data = array();

    public function __construct() {
        parent::__construct();

        // trace error
        // set true to see error log
        $this->output->enable_profiler(false);

        $this->load->model('authlogin');
        if (!$this->authlogin->valid_cookies()) {
            redirect('admin');
        } else {
            $this->data['sess'] = $this->authlogin->get_cookies();
        }
    }

    public function index() {
        $this->load->model('csiswa');
        $this->data['stats'] = $this->csiswa->count_siswa_gel();
        $this->data['ol'] = $this->csiswa->count_siswa_ol();
        $this->data['as'] = $this->csiswa->count_asal();
        $this->data['if'] = $this->csiswa->count_info();
        $this->data['page'] = 'home';
        $this->load->view('dashboard', $this->data);
    }

    public function daftar_all() {
        $this->load->model('csiswa');

        $this->data['sts'] = $this->session->flashdata('sts');
        $this->data['msg'] = $this->session->flashdata('msg');

        $this->data['mhs'] = $this->csiswa->get_all();
        $this->data['page'] = 'daftar_all';
        $this->load->view('dashboard', $this->data);
    }

    public function daftar_lulus() {
        $this->load->model('csiswa');

        $this->data['mhs'] = $this->csiswa->get_lulus();
        $this->data['page'] = 'daftar_lulus';
        $this->load->view('dashboard', $this->data);
    }

    public function daftar_slulus() {
        $this->load->model(array('csiswa', 'jurusan'));

        $this->data['jurs'] = $this->jurusan->get_all();
        $this->data['mhs'] = $this->csiswa->get_blm_lulus();
        $this->data['page'] = 'daftar_slulus';
        $this->load->view('dashboard', $this->data);
    }

    public function sdelete($nodaf) {
        $this->load->model('csiswa');
        if ($que = $this->csiswa->delete($nodaf))
            $msg = "Data Pendaftar $nodaf berhasil dihapus";
        else
            $msg = "Data Pendaftar $nodaf gagal dihapus";

        $this->session->set_flashdata('sts', $que);
        $this->session->set_flashdata('msg', $msg);

        redirect('apps/daftar_all');
    }

    public function sedit($nodaf) {
        $this->load->model(array('csiswa', 'gelombang', 'jurusan'));

        $this->data['gel'] = $this->gelombang->get_gelombang();
        $this->data['mhs'] = $this->csiswa->get($nodaf);
        $this->data['jur'] = $this->jurusan->get_all();

        $post = $this->input->post();
        if ($post) {
            $this->data['sts'] = $this->csiswa->edit_lulus($nodaf, $post);
            $this->data['msg'] = $this->csiswa->get_msg();
        }

        $this->data['page'] = 'edit_lulus';
        $this->load->view('dashboard', $this->data);
    }

    public function tha() {
        $this->load->model('th_pmb');

        $post = $this->input->post();
        if ($post) {
            if ($this->th_pmb->add($post))
                $this->data['sts'] = true;
            else
                $this->data['sts'] = false;

            $this->data['msg'] = $this->th_pmb->get_msg();
        }

        $this->data['tha'] = $this->th_pmb->get();

        $this->data['page'] = 'tha';
        $this->load->view('dashboard', $this->data);
    }

    public function tha_set($id) {
        $this->load->model('th_pmb');
        $this->th_pmb->set_active($id);

        redirect('apps/tha');
    }

    public function jur() {
        $this->load->model('jurusan');

        $this->data['sts'] = $this->session->flashdata('sts');
        $this->data['msg'] = $this->session->flashdata('msg');

        $this->data['jur'] = $this->jurusan->get_all();
        $this->data['page'] = 'jur_view';
        $this->load->view('dashboard', $this->data);
    }

    public function jur_add() {
        $this->load->model('jurusan');

        $post = array();
        $post = $this->input->post();

        if ($post) {
            $this->data['sts'] = $this->jurusan->add($post);
            $this->data['msg'] = $this->jurusan->get_msg();
        }

        $this->data['head'] = 'Tambah Jurusan';
        $this->data['post'] = $post;
        $this->data['page'] = 'jur_form';
        $this->load->view('dashboard', $this->data);
    }

    public function jur_edit($id) {
        $this->load->model('jurusan');

        $post = $this->jurusan->get_by_id($id);
        $this->data['post'] = $post;

        $post = $this->input->post();
        if ($post) {
            $this->data['sts'] = $this->jurusan->edit($id, $post);
            $this->data['msg'] = $this->jurusan->get_msg();
            $this->data['post'] = $post;
        }

        $this->data['head'] = 'Edit Jurusan';
        $this->data['page'] = 'jur_form';
        $this->load->view('dashboard', $this->data);
    }

    public function jur_delete($id) {
        $this->load->model('jurusan');
        if ($que = $this->jurusan->delete($id))
            $msg = "Data Jurusan berhasil dihapus";
        else
            $msg = "Data Jurusan gagal dihapus";

        $this->session->set_flashdata('sts', $que);
        $this->session->set_flashdata('msg', $msg);

        redirect('apps/jur');
    }

    public function gel() {
        $this->load->model('gelombang');

        $this->data['sts'] = $this->session->flashdata('sts');
        $this->data['msg'] = $this->session->flashdata('msg');

        $this->data['gel'] = $this->gelombang->get_gelombang();
        $this->data['page'] = 'gel_view';
        $this->load->view('dashboard', $this->data);
    }

    public function gel_add() {
        $this->load->model('gelombang');

        $post = $this->input->post();
        if ($post) {
            $this->data['sts'] = $this->gelombang->add($post);
            $this->data['msg'] = $this->gelombang->get_msg();
        }

        $this->data['post'] = $post;
        $this->data['head'] = 'Tambah Gelombang PMB';
        $this->data['page'] = 'gel_form';
        $this->load->view('dashboard', $this->data);
    }

    public function gel_edit($id) {
        $this->load->model('gelombang');
        $post = $this->gelombang->get_by_id($id);
        $this->data['post'] = $post;

        $post = $this->input->post();
        if ($post) {
            $this->data['sts'] = $this->gelombang->edit($id, $post);
            $this->data['msg'] = $this->gelombang->get_msg();
            $this->data['post'] = $post;
        }

        $this->data['head'] = 'Edit Gelombang PMB';
        $this->data['page'] = 'gel_form';
        $this->load->view('dashboard', $this->data);
    }

    public function gel_delete($id) {
        $this->load->model('gelombang');
        if ($que = $this->gelombang->delete($id))
            $msg = "Data Gelombang berhasil dihapus";
        else
            $msg = "Data Gelombang gagal dihapus";

        $this->session->set_flashdata('sts', $que);
        $this->session->set_flashdata('msg', $msg);

        redirect('apps/gel');
    }

    public function biaya() {
        $this->load->model('biaya');

        $this->data['sts'] = $this->session->flashdata('sts');
        $this->data['msg'] = $this->session->flashdata('msg');

        $this->data['biaya'] = $this->biaya->get();
        $this->data['page'] = 'biaya_view';
        $this->load->view('dashboard', $this->data);
    }

    public function biaya_add() {
        $this->load->model(array('gelombang', 'jurusan', 'biaya'));

        $this->data['jur'] = $this->jurusan->get_all();
        $this->data['gel'] = $this->gelombang->get_gelombang();

        $post = $this->input->post();
        if ($post) {
            $this->data['sts'] = $this->biaya->add($post);
            $this->data['msg'] = $this->biaya->get_msg();
        }

        $this->data['post'] = $post;
        $this->data['head'] = 'Tambah Biaya PMB';
        $this->data['page'] = 'biaya_form';
        $this->load->view('dashboard', $this->data);
    }

    public function biaya_edit($id) {
        $this->load->model(array('gelombang', 'jurusan', 'biaya'));

        $this->data['jur'] = $this->jurusan->get_all();
        $this->data['gel'] = $this->gelombang->get_gelombang();
        $this->data['post'] = $this->biaya->get_by_id($id);

        $post = $this->input->post();
        if ($post) {
            $this->data['sts'] = $this->biaya->edit($id, $post);
            $this->data['msg'] = $this->biaya->get_msg();
            $this->data['post'] = $post;
        }

        $this->data['head'] = 'Edit Biaya PMB';
        $this->data['page'] = 'biaya_form';
        $this->load->view('dashboard', $this->data);
    }

    public function biaya_del($id) {
        $this->load->model('biaya');
        if ($que = $this->biaya->delete($id))
            $msg = "Data Biaya berhasil dihapus";
        else
            $msg = "Data Biaya gagal dihapus";

        $this->session->set_flashdata('sts', $que);
        $this->session->set_flashdata('msg', $msg);

        redirect('apps/biaya');
    }

    public function input($type = "") {

        $this->data['jkel'] = $this->config->item('jns_kel');
        $this->data['agm'] = $this->config->item('agama');

        $this->load->model(array('jurusan', 'gelombang', 'csiswa', 'th_pmb'));
        $this->data['s1'] = $this->jurusan->get_by_program('S1');
        $this->data['d3'] = $this->jurusan->get_by_program('D3');

        $gel = $this->gelombang->get_gelombang_now();
        $tha = $this->th_pmb->get_tha_aktif();

        $post = array();
        $post = $this->input->post();

        if ($post) {
            if (isset($post['info']))
                $info = implode(',', $post['info']);
            else
                $info = "";

            $jur_d3 = $post['jur_d3'];
            $jur_s1 = $post['jur_s1'];
            unset($post['jur_d3']);
            unset($post['jur_s1']);
            unset($post['info']);

            $post['id_gel'] = $gel['id_gel'];
            $post['nodaf'] = $this->csiswa->get_nodaf($tha['tha']);
            if (isset($post['info']))
                $post['info'] = $info;


            $post['pilihan_jur'] = $jur_d3 . ',' . $jur_s1;

            $post['jkel'] = (!empty($post['jkel'])) ? $post['jkel'] : 0;
            $post['status_nikah'] = (!empty($post['status_nikah'])) ? $post['status_nikah'] : "";
            $post['kelengkapan'] = (!empty($post['kelengkapan'])) ? $post['kelengkapan'] : "0";
            $post['biaya_daftar'] = (!empty($post['biaya_daftar'])) ? $post['biaya_daftar'] : "0";

            #fn insert here..            
            if ($this->csiswa->insert($post)) {
                $this->data['sts'] = true;
                $this->data['msg'] = $this->csiswa->get_msg();
            } else {
                $this->data['sts'] = false;
                if (!isset($gel['id_gel']))
                    $this->data['msg'] = "Belum waktunya pendaftaran PMB, Data gelombang tidak ditemukan";
                else
                    $this->data['msg'] = $this->csiswa->get_msg();
            }


            $post['jur_d3'] = $jur_d3;
            $post['jur_s1'] = $jur_s1;
            $post['info'] = explode(',', $info);
            $this->data['post'] = $post;
        }


        $this->data['head'] = 'Input Data';
        $this->data['post'] = $post;
        $this->data['page'] = ($type == '') ? 'input_form' : "input_detail";
        $this->load->view('dashboard', $this->data);
    }

    public function cetak($nodaf) {

        $this->load->model('csiswa');

        $this->data['mhs'] = $this->csiswa->get($nodaf);
        $this->data['page'] = 'kartu_daftar';
        $this->load->view('cetak', $this->data);
    }

    public function edit($nodaf, $type = '') {
        $this->data['jkel'] = $this->config->item('jns_kel');
        $this->data['agm'] = $this->config->item('agama');

        $this->load->model(array('jurusan', 'gelombang', 'csiswa', 'th_pmb'));
        $this->data['s1'] = $this->jurusan->get_by_program('S1');
        $this->data['d3'] = $this->jurusan->get_by_program('D3');

        $gel = $this->gelombang->get_gelombang_now();
        $tha = $this->th_pmb->get_tha_aktif();

        $post = array();
        $post = $this->csiswa->get($nodaf);
        $post['info'] = explode(',', $post['info']);

        $jur = explode(',', $post['pilihan_jur']);
        $post['jur_d3'] = $jur[0];
        $post['jur_s1'] = $jur[1];

        $this->data['post'] = $post;

        $post = $this->input->post();
        if ($post) {

            if (isset($post['info']))
                $info = implode(',', $post['info']);
            else
                $info = "";

            $post['pilihan_jur'] = $post['jur_d3'] . ',' . $post['jur_s1'];
            unset($post['jur_d3']);
            unset($post['jur_s1']);

            if (isset($post['info']))
                $post['info'] = $info;

            $post['jkel'] = (!empty($post['jkel'])) ? $post['jkel'] : 0;
            $post['status_nikah'] = (!empty($post['status_nikah'])) ? $post['status_nikah'] : "";
            $post['kelengkapan'] = (!empty($post['kelengkapan'])) ? $post['kelengkapan'] : "0";
            $post['biaya_daftar'] = (!empty($post['biaya_daftar'])) ? $post['biaya_daftar'] : "0";

            #fn insert here..            
            if ($this->csiswa->edit($nodaf, $post)) {
                $this->data['sts'] = true;
                $this->data['msg'] = $this->csiswa->get_msg();
            } else {
                $this->data['sts'] = false;
                $this->data['msg'] = $this->csiswa->get_msg();
            }

            $post['info'] = explode(',', $info);
            $post['jur_d3'] = $jur[0];
            $post['jur_s1'] = $jur[1];
            $this->data['post'] = $post;
        }

        $this->data['head'] = 'Edit Data';
        $this->data['page'] = ($type == '') ? 'input_form' : "input_detail";
        $this->load->view('dashboard', $this->data);
    }

    public function lulus($nodaf, $jur) {
        $this->load->model('csiswa');
        if ($jur > 0) {
            if ($this->csiswa->set_lulus($nodaf, $jur))
                return true;
            else
                return false;
        }else {            
            if ($this->csiswa->set_nlulus($nodaf))
                return true;
            else
                return false;
        }
    }

    public function rincian($nodaf) {
        $this->load->model(array('csiswa', 'biaya', 'jurusan'));

        $mhs = $this->csiswa->get($nodaf);
        $this->data['mhs'] = $mhs;
        $this->data['jur'] = $this->jurusan->get_by_id($mhs['id_jurusan']);
        $this->data['bia'] = $this->biaya->get_rincian($mhs['id_gel'], $mhs['id_jurusan']);

        $this->data['page'] = 'rincian_biaya';
        $this->load->view('cetak', $this->data);
    }

    public function spp() {
        $this->load->model('jurusan');
        $this->data['sts'] = $this->session->flashdata('sts');
        $this->data['msg'] = $this->session->flashdata('msg');
        $this->data['jur'] = $this->jurusan->get_spp();

        $this->data['page'] = "spp_view";
        $this->load->view('dashboard', $this->data);
    }

    public function spp_add() {
        $this->load->model('jurusan');
        $post = $this->input->post();

        if ($post) {
            $sts = $this->jurusan->add_spp($post);
            $this->data['msg'] = $this->jurusan->get_msg();
            $this->data['sts'] = $sts;
        }

        $this->data['post'] = $post;
        $this->data['jur'] = $this->jurusan->get_all();
        $this->data['page'] = "spp_form";
        $this->load->view('dashboard', $this->data);
    }

}

